package com.imoocdl.service.impl;

import com.imoocdl.dto.OrderDtDTO;
import com.imoocdl.enums.ResultEnum;
import com.imoocdl.exception.SellException;
import com.imoocdl.service.OrderService;
import com.imoocdl.service.PayService;
import com.imoocdl.utils.JsonUtil;
import com.imoocdl.utils.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.BestPayService;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by pc on 2018/7/20.
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {
    private static final String ORDER_NAME = "微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Autowired
    private OrderService orderService;

    /**
     * 微信统一下单  返回预付单信息
     * @param orderDtDTO
     */
    @Override
    public PayResponse create(OrderDtDTO orderDtDTO) {
        PayRequest payRequest = new PayRequest(); //openid = oTgZpweNnfivA9ER9EIXoHAjlrWQ
        payRequest.setOpenid(orderDtDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDtDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDtDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信发起支付] request={}", JsonUtil.toJson(payRequest));
       PayResponse payResponse =  bestPayService.pay(payRequest);
        log.info("[微信发起支付] payResponse={}" , JsonUtil.toJson(payResponse));
        return payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1,验证签名
        //2,支付的状态
        //3,支付的金额
        //4,支付人(下单人 == 支付人) 不一定成立



        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("[微信支付] 异步通知,payResponse={}",JsonUtil.toJson(payResponse));
        //查询订单
       OrderDtDTO orderDtDTO =  orderService.findOne(payResponse.getOrderId());
       //判断订单是否存在
        if(orderDtDTO == null){
            log.error("[微信支付] 异步通知 订单不存在, orderId={}",payResponse.getOrderId());
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        //判断金额是否一致
        if(!MathUtil.equals(payResponse.getOrderAmount(),orderDtDTO.getOrderAmount().doubleValue())){
            log.info("[微信支付] 异步通知   微信通知金额={},系统金额={}",
                    payResponse.getOrderAmount(),orderDtDTO.getOrderAmount());
             throw new SellException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }
        //修改订单支付状态
        orderService.paid(orderDtDTO);
        return payResponse;
    }

    /**
     * 退款
     * @param orderDtDTO
     */
    @Override
    public RefundRequest refund(OrderDtDTO orderDtDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDtDTO.getOrderId());
        refundRequest.setOrderAmount(orderDtDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("[微信退款] request={}",JsonUtil.toJson(refundRequest));
        RefundResponse refundResponse =  bestPayService.refund(refundRequest);
        log.info("[微信退款] response={}",JsonUtil.toJson(refundResponse));
        return refundRequest;
    }
}
